package pers.hl.module_main.broadcastreceiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import pers.hl.lib_global.KTApplication
import pers.hl.lib_global.common.Const
import pers.hl.lib_global.common.MMKVHelper
import pers.hl.module_main.mine.MineMenu
import pers.hl.module_main.mine.MineMenuActivity
import java.util.*
import kotlin.collections.HashMap

class NotificationReceiver: BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        intent?.apply {
            when (action) {
                Const.NotificationKeys.KEY_EXPIRING_BORROWS -> {
                    val clickIntent = Intent(KTApplication.getInstance(), MineMenuActivity::class.java)
                    clickIntent.putExtra(MineMenuActivity.KEY_MENU_TYPE, MineMenu.MENU_BORROW)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    KTApplication.getInstance().startActivity(clickIntent)
                    cacheClickAction()
                }
            }
        }
    }

    /**
     * 缓存通知点击行为，以点击action为key，当前日期为value
     */
    private fun cacheClickAction() {
        val clickDate = TimeUtils.date2String(Date(), Const.DateFormat.FORMAT_YYYY_MM_DD)
        LogUtils.d("clickDate:${clickDate}")
        val map = HashMap<String, String>()
        map[Const.NotificationKeys.KEY_EXPIRING_BORROWS] = clickDate
        MMKVHelper.saveNotificationClickAction(map)
    }
}